package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;

import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Service
public class UserInfoService {
    /**
     * user----userInfo 表关系，一对一
     */
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FaceEngineService faceEngineService;
    @Autowired
    private PicUploadService picUploadService;
    //保存用户信息
    public Boolean saveUserInfo(Map<String, String> param) {
        //获取user对象
        User user = UserThreadLocal.get();
        if (null==user){
            return false;//匹配失败
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getId());
        Integer count = userInfoMapper.selectCount(wrapper);
        //封装对象
        userInfo.setNickName(param.get("nickname"));
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));
        userInfo.setSex(StringUtils.equalsIgnoreCase(param.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);//性别
        //避免重复录入
        Boolean flag = false;
        if (count==0){
            int insert = userInfoMapper.insert(userInfo);
            flag = true;
        }else {
            int update = userInfoMapper.update(userInfo, wrapper);
            flag = true;
        }
        return flag;
    }

    public Boolean saveUserLogo(MultipartFile file,String token) {
        //获取user对象
        User user = this.userService.queryUserByToken(token);
        if (null==user){
            return false;//匹配失败
        }
     /*   try {
            boolean picture = this.faceEngineService.checkIsPortrait(file.getBytes());//虹软检测是否为人像
            if (!picture){
             return false;//非人像
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/

        //上传图片到阿里云
        PicUploadResult result = this.picUploadService.upload(file);
        if (StringUtils.isEmpty(result.getName())){
            //文件名为空说明上传失败
            return false;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(result.getName());

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getId());
        int update = this.userInfoMapper.update(userInfo, wrapper);
        System.out.println(update);
        return update==1;

    }
}
